<?php

namespace common\traits;

/**
 * 分表关联查询
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
trait SquenceRelationQuery {

    /**
     * Creates a query instance for `has-one` or `has-many` relation.
     * @param string $class the class name of the related record.
     * @param array $link the primary-foreign key constraint.
     * @param bool $multiple whether this query represents a relation to more than one record.
     * @return ActiveQueryInterface the relational query object.
     * @since 2.0.12
     * @see hasOne()
     * @see hasMany()
     */
    protected function createRelationQuery($class, $link, $multiple) {
        /* @var $class ActiveRecordInterface */
        /* @var $query ActiveQuery */
        if (method_exists($class, 'findx')) {
            $sequenceField = $class::$sequenceField;
            $index = array_search($sequenceField, $link);
            if ($index === false && array_key_exists($sequenceField, $link)) {
                $index = $link[$sequenceField];
            }
            $query = $class::findx($this->$index);
        } else {
            $query = $class::find();
        }
        $query->primaryModel = $this;
        $query->link = $link;
        $query->multiple = $multiple;
        return $query;
    }

}
